package com.zte.shopping.action.backend;

import com.zte.shopping.constant.ResponseCodeConstant;
import com.zte.shopping.entity.Dept;
import com.zte.shopping.entity.User;
import com.zte.shopping.exception.DeptExistException;
import com.zte.shopping.exception.LoginDisabledException;
import com.zte.shopping.exception.RequestParameterException;
import com.zte.shopping.service.DeptService;
import com.zte.shopping.util.PageSupport;
import com.zte.shopping.util.ResponseResult;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @program: ssm
 * @description: 管理界面
 * @author: zxb
 * @create: 2020-06-23 21:48
 **/
@Controller
@RequestMapping("dept")
public class BackendDeptManagerController {

    private  static  final Logger logger= Logger.getLogger(BackendDeptManagerController.class);

    @Autowired
    DeptService deptService;

    @RequestMapping("/toDept")
    public String  toDeptManager(@RequestParam(value = "pageIndex",required = false,defaultValue = "1")Integer pageIndex,
                                 Model model){

        logger.debug("enter BackendDeptManagerController------> toDeptManager param: pageIndex"+pageIndex);
        List<Dept> deptList = deptService.deptList(pageIndex);
        logger.debug("enterBackendUserManagerController------> toUserManager UserList : size" + deptList.size());
        for(int i=0;i<deptList.size();i++) {
            Dept dept = deptList.get(i);
            logger.debug("deptId="+dept.getDeptId()+" DeptName="+dept.getDeptName());
        }
        PageSupport pageSupport = new PageSupport();
        pageSupport.setTotalCount(this.deptService.deptListCount());
        pageSupport.setCurrentPageNo(pageIndex);

        model.addAttribute("pageSupport", pageSupport);
        model.addAttribute("DeptList", deptList);

        return "backend/deptManager";
    }

    /**
     * @Author zuti
     * @Description //TODO 添加父部门 部门名称不能重复 部门编号的创建规则:部门前缀BM + 当前年月日  + 序列号
     * @Date 8:41 2020/6/30
     * @Param [deptName, remark, session]
     * @return com.zte.shopping.util.ResponseResult
     **/

    @ResponseBody
    @RequestMapping("/addFatherDept")
    public ResponseResult addFatherDept(String deptName, String remark, HttpSession session)
    {
        ResponseResult result = new ResponseResult();

        try
        {
            deptService.addFatherDept(deptName, remark, session);

            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);

        } catch (DeptExistException e)
        {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);

        } catch (LoginDisabledException e)
        {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_LOGIN_TIMEOUT);

        } catch (Exception e)
        {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);

        }

        return result;
    }

    /**
     * 添加子部门
     *
     * 部门名称不能重复
     * 部门编号创建规则:部门前缀BM + 当前年月日  + 序列号
     */
    @ResponseBody
    @RequestMapping("/addSonDept")
    public ResponseResult addSonDept(String fatherDeptId, String deptName, String remark, HttpSession session)
    {
        ResponseResult result = new ResponseResult();

        try
        {
            deptService.addSonDept(fatherDeptId, deptName, remark, session);

            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);

        } catch (DeptExistException e)
        {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);

        } catch (LoginDisabledException e)
        {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_LOGIN_TIMEOUT);

        } catch (Exception e)
        {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);

        }

        return result;
    }


    /**
     * 跳出 修改部门信息  页面时  根据部门id查询要修改的部门信息
     */
    @ResponseBody
    @RequestMapping("/findById")
    public ResponseResult findById(String id)
    {
        ResponseResult result = new ResponseResult();

        try
        {
            Dept dept = deptService.findById(id);

            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);

            result.setReturnObject(dept);

        } catch (RequestParameterException e)
        {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);

        } catch(Exception e)
        {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }

    /**
     * @Author zuti
     * @Description //TODO 修改部门信息(不区分  父部门  还是  子部门)
     * @Date 8:50 2020/6/30
     * @Param [deptId, deptName, remark]
     * @return com.zte.shopping.util.ResponseResult
     **/

    @ResponseBody
    @RequestMapping("/modifyDept")
    public ResponseResult modifyDept(String deptId, String deptName, String remark)
    {
        ResponseResult result = new ResponseResult();

        try
        {
            deptService.modifyDept(deptId, deptName, remark);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);

        } catch (DeptExistException e)
        {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);

        } catch(Exception e)
        {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }

    /**
     * 子部门的 启用/禁用
     */
    @ResponseBody
    @RequestMapping("/modifyStatus")
    public ResponseResult modifyStatus(String deptId, String isValid)
    {
        ResponseResult result = new ResponseResult();
        try
        {
            deptService.modifyStatus(deptId, isValid);

            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);

        } catch (RequestParameterException e)
        {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);

        } catch(Exception e)
        {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);

        }

        return result;
    }


}
